package scheduler.fixtures;

import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import se392.ateam2006.meetingmgmt.MeetingMgmtRemote;
import se392.ateam2006.meetingmgmt.meeting.MeetingEntity;
import se392.ateam2006.resourcemgmt.room.RoomEntity;

/**
 * Tests for retrieving and formatting Meeting entities. 
 * @author Ateam (Matthew Bennett, Claire Melton, Shingai Manyiwa, John Adderley)
 * @version 25/03/07
 */
public class PlannedMeetings extends fit.RowFixture{

    private MeetingMgmtRemote meetings;
    
    /** Creates a new instance of PlannedMeetings */
    public PlannedMeetings(MeetingMgmtRemote meetings) {
        this.meetings = meetings;
    }
    
    public Object[] query() throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd kk:mm");
        SimpleDateFormat durationFormat = new SimpleDateFormat("s H:mm");
        ArrayList meetingList = new ArrayList();
        Collection <MeetingEntity> tempMees = meetings.getAllMeetings();
        String theVenue;

        
        for (MeetingEntity mee : tempMees) {

            meetingList.add(new MeetingShell(
                    mee.getInitiator().getEmail(),
                    mee.getId(),
                    format.format(mee.getStartDateRange().getTime()),
                    format.format(mee.getEndDateRange().getTime()),
                    mee.getDuration().toString(),
                    mee.getTitle(),
                    mee.getDescription(),
                    this.extractVenue(mee.getRoom()),
                    mee.getRequestedRoomSize()
                    ));
            
        }
        return meetingList.toArray();
        
    }
    
    private String extractVenue(RoomEntity room){
        if(room == null || room.getRoomID()==null)
            return "";
        else
            return room.getRoomID();
    }
    
    public Class getTargetClass() {
        return MeetingShell.class;
    }
    
}
